/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

function UsersInRolesViewModel() {
    var self = this;
    // put a property loop here
    self.UsersInRoleID = ko.observable();
    self.TS = ko.observable();
    self.CreatedBy = ko.observable();
    self.CreatedDt = ko.observable();
    self.ModifiedBy = ko.observable();
    self.ModifiedDt = ko.observable();
    self.UserId = ko.observable();
    self.ApplicationRoleId = ko.observable();
    self.Action = ko.observable();
    self.Caller = ko.observable();
    self.Role = ko.observable();
    self.PageSize = ko.observable();
    self.PageNumber = ko.observable();
    self.SortColumn = ko.observable();
    self.SortType = ko.observable();
    self.TotalRows = ko.observable();

    self.UsersInRolesList = ko.observableArray([]);
    self.setUsersInRoles = function (
			UsersInRoleID, TS, CreatedBy, CreatedDt, ModifiedBy, ModifiedDt, UserId, ApplicationRoleId, _Action, _Caller, _Role, _PageNumber, _PageSize, _SortColumn, _SortType, _TotalRows

	) {

        UsersInRoleID = _UsersInRoleID;
        TS = _TS;
        CreatedBy = _CreatedBy;
        CreatedDt = _CreatedDt;
        ModifiedBy = _ModifiedBy;
        ModifiedDt = _ModifiedDt;
        UserId = _UserId;
        ApplicationRoleId = _ApplicationRoleId;
        Action = _Action;
        Caller = _Caller;
        Role = _Role;
        PageSize = _PageSize
        PageNumber = _PageNumber;
        SortColumn = _SortColumn;
        SortType = _SortType;
        TotalRows = _TotalRows;

    }; //  End of set procedure 

    //Add User Roles
    self.AddUsersInRoles = function () {

        alert("We are in the AddUsersInRoles ko function");

        this.Action = "Add";
        this.Caller = "trooks";
        this.Role = "user";

        jQuery.ajax({
            url: "/api/UsersInRolesapi/",
            type: 'post',
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, CreatedBy: self.CreatedBy, CreatedDt: self.CreatedDt, ModifiedBy: self.ModifiedBy, ModifiedDt: self.ModifiedDt, UserId: self.UserId, ApplicationRoleId: self.ApplicationRoleId },
            contentType: 'application/json',
            success: function (result) {
                alert(result);
            }
        });



    }; //End of add procedure
    //Load User Roles
    self.LoadUserinroles = function (roleVal) {
        self = roleVal;
        alert("Load Roles " + self.UsersInRoleID);

    };

    //Delete User Roles
    self.DeleteUserinroles = function () {
        //alert("Delete Action " + self.UsersInRoleID);
        self.Action = "Delete";
        self.Caller = "trooks";
        self.Role = "user";

        jQuery.ajax({
            url: "/api/UsersInRolesapi/",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, CreatedBy: self.CreatedBy, CreatedDt: self.CreatedDt, ModifiedBy: self.ModifiedBy, ModifiedDt: self.ModifiedDt, UserId: self.UserId, ApplicationRoleId: self.ApplicationRoleId },
            type: 'POST'


        }).done(function (data) {

            $.each(data, function (key, val) {
                alert("We have loaded the values and are in the each module. " + JSON.stringify(val));

            })
        })
    };
    //Edit User Roles
    self.UpdateUserinroles = function () {

        alert("We are in the Update Roles " + self.CreatedBy);

        self.Action = "Load";
        self.Caller = "trooks";
        self.Role = "user";

        var myData = ko.toJSON(self);
        jQuery.ajax({
            url: "/api/UsersInRolesapi/",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, CreatedBy: self.CreatedBy, CreatedDt: self.CreatedDt, ModifiedBy: self.ModifiedBy, ModifiedDt: self.ModifiedDt, UserId: self.UserId, ApplicationRoleId: self.ApplicationRoleId },
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                alert(result);
            }
        });



    }; //End of add procedure

    self.GetUsersInRolesByParameter = function () {
        alert('u are in here');
        this.Action = "GetAll";
        this.Caller = "trooks";
        this.Role = "user";
        this.PageNumber = 1;
        this.PageSize = 10;
        this.SortColumn = "UsersInRoleID";
        this.SortType = "asc";
        var myData = ko.toJSON(this);
        jQuery.ajax({
            url: "/api/UsersInRolesapi/",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
            type: 'get'

        }).done(function (data) {

            $.each(data, function (key, val) {

                self.UsersInRolesList.push(
                    val
                )
            })
        });


    };


}; //End of view model





